package com.xy.exam.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.xy.exam.pojo.dto.UserAddNewDTO;
import com.xy.exam.pojo.dto.UserLoginDTO;
import com.xy.exam.pojo.dto.UserUpdateDTO;
import com.xy.exam.service.IUserService;
import com.xy.exam.web.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Slf4j
@RequestMapping("/user")
@RestController
@Api(tags="01.用户管理")//添加模块名称
public class UserController {

    @Autowired
    IUserService userService;

    @PostMapping("/login")
    public JsonResult login(UserLoginDTO userLoginDTO) {
        log.debug("开始处理【管理员登录】的请求，参数：{}", userLoginDTO);
        String jwt = userService.login(userLoginDTO);
        return JsonResult.ok(jwt);
    }
    @PostMapping("/add-new")
    @ApiOperation(value = "添加用户")//添加名称
    @ApiOperationSupport(order = 100)//排序
    public JsonResult addNew( UserAddNewDTO userAddNewDTO) {
        userService.addNew(userAddNewDTO);
        return new JsonResult().ok();
    }
    @PostMapping("/{id:[0-9]+}/deleteById")
    @ApiOperation("删除用户,根据id")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "long")
    })
    public JsonResult delete(@Range(min = 1, message = "删除用户失败，ID值无效！")
                             @PathVariable Long id) {
        userService.delete(id);
        return JsonResult.ok();
    }
    @PostMapping("/{id:[0-9]+}/update")
    @ApiOperation("修改用户信息")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "long")
    })
    public JsonResult updateInfoById(@Range(min = 1, message = "修改用户信息失败，ID值无效！")
                                     @PathVariable Long id, @Valid UserUpdateDTO userUpdateDTO) {
        userService.updateInfoById(id, userUpdateDTO);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/enable")
    @ApiOperation("启用用户")
    @ApiOperationSupport(order = 310)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "long")
    })
    public JsonResult setEnable(@Range(min = 1, message = "启用用户失败，ID值无效！")
                                @PathVariable Long id) {
        userService.setEnable(id);
        return JsonResult.ok();
    }


    @PostMapping("/{id:[0-9]+}/disable")
    @ApiOperation("禁用用户")
    @ApiOperationSupport(order = 311)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "long")
    })
    public JsonResult setDisable(@Range(min = 1, message = "禁用用户失败，ID值无效！")
                                 @PathVariable Long id) {
        userService.setDisable(id);
        return JsonResult.ok();
    }


    @GetMapping("/{id:[0-9]+}/selectById")
    @ApiOperation("查询相册详情,根据id")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "long")
    })
    public JsonResult getStandardById(@Range(min = 1, message = "获取用户信息失败，ID值无效！")
                                      @PathVariable Long id) {

        return JsonResult.ok( userService.getStandardById(id));
    }


    @GetMapping("/list")
    @ApiOperation("查询用户列表")
    @ApiOperationSupport(order = 420)
    public JsonResult list() {

        return JsonResult.ok(userService.list());
    }
}
